package com.linkage.educloud.ah.mvp.model;


import com.linkage.educloud.ah.MyApplication;
import com.linkage.educloud.ah.bean.AccountData;
import com.linkage.educloud.ah.http.Method;
import com.linkage.educloud.ah.mvp.contract.ILoginContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallClazzProxy;
import com.zhouyou.http.model.ApiResult;
import com.zhouyou.http.model.HttpParams;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;


/**
 * <desc>
 * Created by The Moss on 2018/7/26.
 */

public class LoginModel implements ILoginContract.IModel {

    @Override
    public Observable request(Method method, boolean isToken, HttpParams params) {
        return EasyHttp.post("")
                .params(params)
                .accessToken(isToken)
//                        .sign(true)
                .execute(new CallClazzProxy<ApiResult<String>, String>(String.class) {
                })
                .subscribeOn(Schedulers.io());
    }

    @Override
    public Observable lastLoginAccount() {
        return Observable.create(new ObservableOnSubscribe<List<AccountData>>() {
            @Override
            public void subscribe(ObservableEmitter<List<AccountData>> e) {
                try {
                    List<AccountData> accounts = MyApplication.getInstance().getResovler().getLastAccount();
                    if (accounts == null) {
                        e.onError(new Throwable("no login account data"));
                    } else {
                        e.onNext(accounts);
                    }
                    e.onComplete();
                } catch (Exception e1) {
                    e1.printStackTrace();
                    e.onError(new Throwable(e1.getMessage()));
                }
            }
        });
    }
}
